/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: common
 * Description: Common classes
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.common.model;

import java.util.Arrays;

/**
 * Directory and file flags.
 *
 * @author Rage Steel
 */
public enum EntryFlag {
    // Do not reorder elements in this enum!
    // Reodering will be affected on ordinal of elements!

    /**
     * 0, Video format.
     */
    VIDEO("avi", "asf", "mp4", "mpg", "mpeg", "mov", "wmv", "ifo", "swf", "rv",
        "mkv", "flv"),

    /**
     * 1, Audio format
     */
    AUDIO("mp1", "mp2", "mp3", "m4a", "wav", "voc", "snd", "aud", "wma", "ogg",
        "aac", "au", "s3m", "mod", "ra", "mid"),

    /**
     * 2, Picture format.
     */
    PICTURE("jpg", "jpeg", "jpe", "png", "gif", "bmp", "ico", "psd", "cur",
        "ani", "nlm", "pcx", "tif", "tiff"),

    /**
     * 3, Archive format.
     */
    ARCHIVE("rar", "arj", "zip", "lzh", "7z", "cab", "tar", "gzip", "gz",
        "msi", "bz", "bz2", "ace", "ice"),

    /**
     * 4, Disk image format.
     */
    IMAGE("iso", "vc4", "nrg", "img", "cue", "cdi"),

    /**
     * 5, Document format
     */
    DOCUMENT("txt", "doc", "xls", "pdf", "tex", "htm", "html", "hta",
        "shtm", "shtml", "chm", "hlp");

    private final String[] exts;

    private EntryFlag(String... exts) {
        this.exts = exts;
        Arrays.sort(this.exts);
    }

    /**
     * Return EntryFlag by extension.
     *
     * @param ext file extension in lowercase
     * @return found Flag or NULL
     */
    public static EntryFlag byExt(String ext) {
        for (EntryFlag entryFlag: values()) {
            if (Arrays.binarySearch(entryFlag.exts, ext) >= 0) {
                return entryFlag;
            }
        }
        return null;
    }
}
